package com.biostime.springdraw.modules.sd.service;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class ExportService {
	/**
	   * 将数据导出到excel中
	   * @param data 将要被导入到excel中的数据
	   * @throws IOException
	   */
	  @SuppressWarnings("resource")
	public void crateTempFile(ArrayList<LinkedHashMap<String, Object>> data,HttpServletRequest request,HttpServletResponse response) throws IOException {
	    //建立一个excel工作簿
	    HSSFWorkbook wb = new HSSFWorkbook();
	    //建立一个sheet
	    HSSFSheet sheet = wb.createSheet("test");
	    //建立一个单元格样式
	    HSSFCellStyle commonTitileStyle = wb.createCellStyle();
	    //建立一个字体样式
	    HSSFFont font=wb.createFont();
	    //设置字体颜色
	    font.setColor(HSSFColor.RED.index);//HSSFColor.VIOLET.index //字体颜色
	    //设置字体的大小
	    font.setFontHeightInPoints((short)12);
	    //字体增粗
	    font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
	    //将字体样式放入单元格样式中
	    commonTitileStyle.setFont(font);
	    
	    //建立一行
	    HSSFRow titleRow = sheet.createRow(0);
	    Map<String, Object> titleMap = data.get(0);
	    Set<String> keySet = titleMap.keySet();
	    Iterator<String> it = keySet.iterator();
	    
	    //sheet中行索引值
	    int indexOfRow = 0;
	    
	    //建立第一行第一个单元格
	    HSSFCell titileCell = titleRow.createCell(indexOfRow);
	    String titilKey = "合生元抽奖获奖名单";
	    
	    //设置单元格宽度
	    sheet.setColumnWidth(indexOfRow, 20 * 256);
	    
	    //给单元格赋值
	    titileCell.setCellValue(titilKey);
	    
	    //设置单元格样式
	    titileCell.setCellStyle(commonTitileStyle);	    
	    indexOfRow = 1;
	    
	    //设置sheet第二行
	    HSSFRow dataRow = sheet.createRow(indexOfRow);
	    
	    while (it.hasNext()) {
	    	
	      //遍历建立第二行单元格格式
	      HSSFCell cell1 = dataRow.createCell(indexOfRow-1);
	      String key1 = it.next();
	      
	      //默认单元格大小
	      int  height = 20;
	      int width= 256;
	      if(key1.equals("收货地址")){
	    	  width = 430;
		   }    
	      else if( key1.equals("描述")){
	    	  width = 420;
		  }	   
	      else if( key1.equals("customerid")){
	    	  width = 140;
		  }	  	      
	      else if( key1.equals("昵称")){
	    	  width = 140;
		  }	  
	      else if( key1.equals("手机号码")){
	    	  width = 200;
		  }	 	      
	      else if( key1.equals("收件人")){
	    	  width = 140;
		  }	  
	      else if( key1.equals("收件电话")){
	    	  width = 200;
		  }	 
	      else if(key1.equals("微信openid")){
	    	  width = 380;
	      }
	      else if(key1.equals("是否中奖")){
	    	  width = 150;
	      }
	      else if(key1.equals("是否已领奖")){
	    	  width = 150;
	      }	      
	      sheet.setColumnWidth(indexOfRow-1, height * width);
	      cell1.setCellValue(key1);
	      indexOfRow++;
	    }
	    
	    //设置第三行
	    indexOfRow = 2;
	    for (Map<String, Object> temp : data) {
	      HSSFRow titleRow1 = sheet.createRow(indexOfRow);
	      Set<String> set = temp.keySet();
	      Iterator<String> it1 = set.iterator();
	      int indexOfColumn = 0;
	      while (it1.hasNext()) {
	          String key = it1.next();
	          HSSFCell cell = titleRow1.createCell(indexOfColumn);
	          cell.setCellValue(temp.get(key).toString());
	          indexOfColumn++;
	      }
	      indexOfRow++;
	    }
	       //设置文件MIME类型  
	       response.setContentType("application/vnd.ms-excel	application/x-excel");  
	       //设置Content-Disposition  
	       response.setHeader("Content-Disposition", "attachment;filename="+"winner.xls");  
	       OutputStream fos = response.getOutputStream();
	       wb.write(fos);
	       fos.close();
	  }
}
